/**
 * Created by wxb on 2016/10/21.
 */
(function () {
  'use strict';

  angular.module('BlurAdmin')
    .controller('LoginPageCtrl', LoginPageCtrl);

  /** @ngInject */
  function LoginPageCtrl($scope, $http, toastr, $state, localStorageService, md5) {
    $scope.title = '运营管理平台';
    $scope.formData = {};
    $scope.login = function () {
      $http({method: 'POST', data: {mobile: $scope.formData.username, password: md5.createHash($scope.formData.password)}, url: "/kt/admin/staff/login", timeout: 100000}).then(function(res) {
        if (res.data && res.data.errcode === 0) {
          console.log(res.data);
          toastr.success('登陆成功', '成功!');
          var menus = [];
          var subMenus1 = [
            {
              "parent_id": "0",
              "name": "激活码管理",
              "sort": 20,
              "is_show": "1",
              "is_activiti": "1",
              "permission": "激活码管理",
              "id": '010',
            },
            {
              "parent_id": "010",
              "name": "酒店激活码",
              "sort": 1,
              "is_show": "1",
              "is_activiti": "1",
              "permission": "酒店激活码",
              "id": '011',
            },
            {
              "parent_id": "010",
              "name": "机票激活码",
              "sort": 2,
              "is_show": "1",
              "is_activiti": "1",
              "permission": "机票激活码",
              "id": '012',
            },
          ];
          var subMenus2 = [
            {
              "parent_id": "0",
              "name": "酒店管理",
              "sort": 30,
              "is_show": "1",
              "is_activiti": "1",
              "permission": "酒店管理",
              "id": '020',
            },
            {
              "parent_id": "020",
              'parentName': '酒店管理',
              "name": "酒店订单",
              "sort": 1,
              "is_show": "1",
              "is_activiti": "1",
              "permission": "酒店订单",
              "id": '021',
            },
            {
              "parent_id": "020",
              'parentName': '酒店管理',
              "name": "机票订单",
              "sort": 2,
              "is_show": "1",
              "is_activiti": "1",
              "permission": "机票订单",
              "id": '022',
            },
          ];
          if (res.data.data.companyType == 1) {
            menus = subMenus1.concat(subMenus2);
          }
          else if (res.data.data.companyType == 2) {
            menus = subMenus2;
          }
          else {
            menus = [
              {
                "parent_id": "0",
                "name": " 人员管理",
                "sort": 10,
                "is_show": "1",
                "is_activiti": "1",
                "permission": "人员管理",
                "id": '000',
              },
              {
                "parent_id": "000",
                'parentName': '人员管理',
                "name": "公司管理",
                "sort": 1,
                "is_show": "1",
                "is_activiti": "1",
                "permission": "公司管理",
                "id": '001',
              },
              {
                "parent_id": "000",
                'parentName': '人员管理',
                "name": "人员管理",
                "sort": 2,
                "is_show": "1",
                "is_activiti": "1",
                "permission": "人员管理",
                "id": '002',
              },
            ].concat(subMenus1).concat(subMenus2);
          }
          menus = _.uniq(menus, 'id');
          localStorageService.set('menus', menus);
          var userInfo = res.data.data;
          localStorageService.cookie.set('user', userInfo);
          console.log(localStorageService.get('menus'));
          if (res.data.data.companyType == 0) {
            $state.go('admin.staffManage.staffList')
          }
          if (res.data.data.companyType == 1) {
            $state.go('admin.codeManage.hotelCode')
          }
          if (res.data.data.companyType == 2) {
            $state.go('admin.hotelManage.hotelRecord')
          }
        }
        else {
          toastr.error(res.data.errmsg, '失败!');
        }
      }, function (err) {
        toastr.error(err.data.errmsg, '失败!');
      })


    }

  }

})();


